package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.WGood;
import com.ruoyi.system.domain.WGoodsRecords;
import com.ruoyi.system.domain.WOrderGood;
import com.ruoyi.system.domain.vo.WGoodVO;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.WGoodMapper;
import com.ruoyi.system.mapper.WGoodsRecordsMapper;
import com.ruoyi.system.mapper.WOrderGoodMapper;
import com.ruoyi.system.service.IWGoodService;

/**
 * 物品Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-05
 */
@Service
public class WGoodServiceImpl implements IWGoodService 
{
    @Autowired
    private WGoodMapper wGoodMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private WGoodsRecordsMapper wGoodsRecordsMapper;
    @Autowired
    private WOrderGoodMapper wOrderGoodMapper;

    /**
     *   `is_in` int(4) DEFAULT '1' COMMENT '出入库，  1.使用（生产使用）  2.盘点  3入库  4退回 ',
  `change_number` int(4) DEFAULT NULL COMMENT '变化数量',
  `in_type` int(4) NOT NULL DEFAULT '0' COMMENT '出入库类型（ 1 出库 2入库） 0代表处理锁定库存的或者 不动库存的',
     */
    @Override
    public void dealWGood(WGoodsRecords wGoodsRecords) {
    	Long goodsId = wGoodsRecords.getGoodsId();
    	WGood selectWGoodById = wGoodMapper.selectWGoodById(goodsId);
    	wGoodsRecords.setGoodsName(selectWGoodById.getGoodsName());
    	Integer changeNumber = wGoodsRecords.getChangeNumber();
    	if(changeNumber == null || changeNumber <=0 ) throw new RuntimeException("没传数量");
    	Integer isIn = wGoodsRecords.getIsIn();
    	if(isIn == null || isIn <=0 ) throw new RuntimeException("没传isIn");
    	Integer getInType = wGoodsRecords.getInType();
    	if(getInType == null || getInType <=0 ) throw new RuntimeException("没传inType");
     
    	wGoodsRecords.setCreateTime(new Date());
    	wGoodsRecords.setDeptId(selectWGoodById.getDeptId());
    	
    	wGoodsRecords.setCustomerId(selectWGoodById.getCustomerId());
    	wGoodsRecordsMapper.insertWGoodsRecords(wGoodsRecords);
    	wGoodMapper.updateWGoodNum(wGoodsRecords);
    }
    
    
    
    @Override
    public WGoodVO selectWGoodByIdNew(Long id) {
    	WGood selectWGoodById = wGoodMapper.selectWGoodById(id);
    	WGoodVO wGoodVO = new WGoodVO();
    	BeanUtils.copyBeanProp(wGoodVO, selectWGoodById);
    	Long customerId = wGoodVO.getCustomerId();
    	SysUser selectUserById = sysUserMapper.selectUserById(customerId);
    	
    	wGoodVO.setCustomerPhone(selectUserById.getPhonenumber()); 
    	
    	return wGoodVO;
    }
    
    
    
    /**
     * 查询物品
     * 
     * @param id 物品主键
     * @return 物品
     */
    @Override
    public WGood selectWGoodById(Long id)
    {
        return wGoodMapper.selectWGoodById(id);
    }

    /**
     * 查询物品列表
     * 
     * @param wGood 物品
     * @return 物品
     */
    @Override
    public List<WGoodVO> selectWGoodList(WGoodVO wGood)
    {
        List<WGoodVO> selectWGoodListNew = wGoodMapper.selectWGoodListNew(wGood);
		
        for (WGoodVO wGoodVO : selectWGoodListNew) {
        	Long id = wGoodVO.getId();
        	WOrderGood selectWOrderGoodNum = wOrderGoodMapper.selectWOrderGoodNum(id);
        	Long number = selectWOrderGoodNum.getNumber();
        	Long numberGood = wGoodVO.getNumber();
        	wGoodVO.setNeedNum((number - numberGood) > 0l ? (number - numberGood) : 0l );
		}
        
        return selectWGoodListNew;
    }

    /**
     * 新增物品
     * 
     * @param wGood 物品
     * @return 结果
     */
    @Override
    public int insertWGood(WGood wGood)
    {
        wGood.setCreateTime(DateUtils.getNowDate());
        return wGoodMapper.insertWGood(wGood);
    }

    /**
     * 修改物品
     * 
     * @param wGood 物品
     * @return 结果
     */
    @Override
    public int updateWGood(WGood wGood)
    {
        wGood.setUpdateTime(DateUtils.getNowDate());
        return wGoodMapper.updateWGood(wGood);
    }


    /**
     * 删除物品信息
     * 
     * @param id 物品主键
     * @return 结果
     */
    @Override
    public R delete(Long id)
    {
    	WGood selectWGoodById = wGoodMapper.selectWGoodById(id);
    	Long number = selectWGoodById.getNumber();
    	if(number != null && number >0 ) return R.error("有库存删除不掉");
    	selectWGoodById.setIsDelete(1l);
    	wGoodMapper.updateWGood(selectWGoodById);
        return R.success();
    }
}
